package com.sdb.fee.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sdb.common.annotation.Log;
import com.sdb.common.core.controller.BaseController;
import com.sdb.common.core.domain.AjaxResult;
import com.sdb.common.enums.BusinessType;
import com.sdb.fee.domain.TdPay;
import com.sdb.fee.service.ITdPayService;
import com.sdb.common.utils.poi.ExcelUtil;
import com.sdb.common.core.page.TableDataInfo;

/**
 * 缴费信息Controller
 * 
 * @author yyh
 * @date 2025-02-07
 */
@RestController
@RequestMapping("/fee/pay")
public class TdPayController extends BaseController
{
    @Autowired
    private ITdPayService tdPayService;

    /**
     * 查询缴费信息列表
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @GetMapping("/list")
    public TableDataInfo list(TdPay tdPay)
    {
        startPage();
        List<TdPay> list = tdPayService.selectTdPayList(tdPay);
        return getDataTable(list);
    }

    /**
     * 查询用户的缴费信息列表
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @GetMapping("/userPay")
    public TableDataInfo userPay(TdPay tdPay)
    {
        startPage();
        List<TdPay> list = tdPayService.userPay(tdPay);
        return getDataTable(list);
    }

    /**
     * 导出缴费信息列表
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "缴费信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TdPay tdPay)
    {
        List<TdPay> list = tdPayService.selectTdPayList(tdPay);
        ExcelUtil<TdPay> util = new ExcelUtil<TdPay>(TdPay.class);
        util.exportExcel(response, list, "缴费信息数据");
    }

    /**
     * 获取缴费信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tdPayService.selectTdPayById(id));
    }

    /**
     * 新增缴费信息
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "缴费信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TdPay tdPay)
    {
//        AjaxResult result = toAjax(tdPayService.insertTdPay(tdPay));
//        return toAjax(tdPayService.insertTdPay(tdPay));
        // 仅调用一次 Service 方法，避免重复插入
        int generatedId = tdPayService.insertTdPay(tdPay);
        // 使用 success(Object data) 方法返回 ID
        return AjaxResult.success("操作成功", generatedId);
    }

    /**
     * 退费
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @PostMapping("/refund")
    public AjaxResult refund(@RequestBody TdPay tdPay)
    {
        return toAjax(tdPayService.refund(tdPay));
    }


    /**
     * 修改缴费信息
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "缴费信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TdPay tdPay)
    {
        return toAjax(tdPayService.updateTdPay(tdPay));
    }

    /**
     * 删除缴费信息
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "缴费信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tdPayService.deleteTdPayByIds(ids));
    }
}
